package com.gaga.console.core.service.impl;

import com.gaga.common.enums.AppVersionNew;
import com.gaga.console.base.mybatisplus.LambdaQueryWrapperExt;
import com.gaga.console.base.service.impl.SuperServiceImpl;
import com.gaga.console.core.entity.AppVersion;
import com.gaga.console.core.entity.dto.AppVersionCheckDto;
import com.gaga.console.core.entity.vo.AppVersionCheckVo;
import com.gaga.console.core.mapper.AppVersionMapper;
import com.gaga.console.core.service.AppVersionService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author liujia
 * @since 2023-12-12
 */
@Service
public class AppVersionServiceImpl extends SuperServiceImpl<AppVersionMapper, AppVersion> implements AppVersionService {

    @Override
    public boolean setAppNewVersion(Integer id, Integer isNew) {

        //先更新所有的都为不是最新版本
        AppVersion appVersion = new AppVersion();
        appVersion.setIsNew(AppVersionNew.NOK.getCode());
        LambdaQueryWrapperExt<AppVersion> wraps = new LambdaQueryWrapperExt<>();
        this.baseMapper.update(appVersion, wraps);

        AppVersion appVersionNew = new AppVersion();
        appVersionNew.setIsNew(isNew);
        appVersionNew.setId(id);
        this.baseMapper.updateById(appVersionNew);
        return true;
    }

    @Override
    public AppVersionCheckDto checkHasNewVersion(AppVersionCheckVo entity) {
        //和最新的版本比较版本号是不是相同，如果是大于传过来的版本号 那么提示升级
        LambdaQueryWrapperExt<AppVersion> wraps = new LambdaQueryWrapperExt<>();
        wraps.eq(AppVersion::getIsNew, AppVersionNew.OK.getCode());
        AppVersion appVersionNew = this.baseMapper.selectOne(wraps);
        if (appVersionNew == null) {
            return null;
        }
        if (appVersionNew.getVersionCode() >= appVersionNew.getVersionCode().intValue()) {
            AppVersionCheckDto appVersionCheckDto = new AppVersionCheckDto();
            appVersionCheckDto.setVersionCode(appVersionNew.getVersionCode());
            appVersionCheckDto.setVersionName(appVersionNew.getVersionName());
            appVersionCheckDto.setUpdateType(appVersionNew.getUpdateType());
            appVersionCheckDto.setVersionInfo(appVersionNew.getVersionDesc());
            appVersionCheckDto.setDownloadUrl(appVersionNew.getAndroidDownloadUrl());
            return appVersionCheckDto;
        }
        return null;
    }
}
